﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using System.Reflection;

namespace VCommons
{
    public static class LogHelper
    {

        /// <summary>
        /// 添加日志文件
        /// </summary>
        /// <param name="text">要添加的内容</param>
        /// <param name="fileName">文件的相对路径需带后缀名</param>
        /// <returns>1:</returns>
        public static bool AddLogFiles(this string text, string fileName)
        {
            try
            {
                string filePath = System.Web.HttpContext.Current.Server.MapPath(fileName);
                string dir = filePath.Substring(0, filePath.LastIndexOf("\\"));
                if (!System.IO.Directory.Exists(dir))
                {
                    System.IO.Directory.CreateDirectory(dir);
                }
                using (System.IO.StreamWriter srFile = new System.IO.StreamWriter(filePath, true))
                {
                    srFile.WriteLine(text);
                    srFile.Close();
                    srFile.Dispose();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;

            }


        }

        /// <summary>
        /// 添加日志文件
        /// </summary>
        /// <param name="text">要添加的内容</param>
        /// <param name="fileName">文件的相对路径需带后缀名</param>
        /// <param name="isService">1：服务器自动执行时调用，0：服务器不自动执行时调用</param>
        /// <returns></returns>
        public static bool AddLogFiles(this string text, string fileName, int isService)
        {
            try
            {
                string filePath = string.Empty;
                if (isService > 0)
                {
                    filePath = System.AppDomain.CurrentDomain.BaseDirectory + fileName;
                }
                else
                {
                    filePath = System.Web.HttpContext.Current.Server.MapPath(fileName);
                }
                string dir = filePath.Substring(0, filePath.LastIndexOf("\\"));
                if (!System.IO.Directory.Exists(dir))
                {
                    System.IO.Directory.CreateDirectory(dir);
                }
                using (System.IO.StreamWriter srFile = new System.IO.StreamWriter(filePath, true))
                {
                    srFile.WriteLine(text);
                    srFile.Close();
                    srFile.Dispose();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;

            }


        }
        /// <summary>
        ///实体类值反射
        /// </summary>
        /// <param name="o">对象</param>
        /// <returns></returns>
        public static string WriteObject(this object o)
        {
            if (o == null)
            {
                return string.Empty;
            }
            StringBuilder sb = new StringBuilder();
            Type type = o.GetType();
            if (type.IsClass && type != typeof(string))
            {
                foreach (PropertyInfo p in type.GetProperties())
                {
                    sb.Append(p.Name + ":" + p.GetValue(o, null) + "\r\n");
                }
            }
            else
            {
                sb.Append(o.ToString());
            }
            return sb.ToString();
        }
    }
}
